// 管理用户数据相关
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { setTimeStamp } from '@/utils/auth'
import router from '@/router'
// import { login } from "@/api/user"
export const useUserStore = defineStore(
  'user',
  () => {
    // 1. 定义管理用户数据的state
    const userInfo = ref({})
    const hasUserInfo = ref(false)
    // 2. 定义获取接口数据的action函数
    const getUserInfo = async (info, token) => {
      userInfo.value = { ...info, token }

      // 保存登录时间
      setTimeStamp()
    }
    const getHasUserInfo = (bol) => {
      hasUserInfo.value = bol
    }
    const updateUserInfo = (data) => {
      userInfo.value = { ...userInfo.value, ...data }
    }
    // 退出时清除用户信息
    const clearUserInfo = () => {
      userInfo.value = {}
      hasUserInfo.value = false
      router.push('/login')
    }
    // 3. 以对象的格式把state和action return
    return {
      userInfo,
      hasUserInfo,
      getUserInfo,
      clearUserInfo,
      getHasUserInfo,
      updateUserInfo
    }
  },
  {
    persist: true
  }
)
